/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.TimeZone;

/**
 *
 * @author kien.tran
 */
public class StringUtils {

    public static String[] splitAdvance(String s, String key) {
        String[] re = new String[]{};
        String st;
        if (!contains(s, key)) {
            return addString2Array(re, s);
        }
        do {
            st = s.substring(0, s.indexOf(key));
            re = addString2Array(re, st);
            s = s.substring(s.indexOf(key) + key.length());
        } while (contains(s, key));
        re = addString2Array(re, s);
        return re;
    }

    public static String[] addString2Array(String[] arr, String s) {
        String[] temp = new String[arr.length + 1];
        System.arraycopy(arr, 0, temp, 0, arr.length);
        temp[temp.length - 1] = s;
        return temp;
    }

    public static boolean contains(String s, String key) {


        if (s.length() < key.length() || key.length() == 0) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            // search the character start with 'key'
            if (s.charAt(i) == key.charAt(0)) {
                // check lenght of the rest of string is less than lenght of key
                if ((s.length() - (i + 1)) < (key.length() - 1)) {
                    return false;
                }
                // check the all characters of key are equals with the string's one
                for (int j = 1; j < key.length(); j++) {
                    // return if not macth
                    if (s.charAt(i + j) != key.charAt(j)) {
                        return contains(s.substring(i + j), key);
                    }
                }
                // if all of them match
                return true;
            }
        }
        return false;
    }

    public static String replace(String text, String oldString, String newString) {
        StringBuffer sb = new StringBuffer();

        int pos = text.indexOf(oldString);

        if (pos != -1) {
            sb.append(text.substring(0, pos));
            sb.append(newString);
            sb.append(text.substring(pos + oldString.length(), text.length()));
        }

        return sb.toString();
    }

    public static String getValue(String s, String delimiter) {
        String result = null;
        int index = s.indexOf(delimiter);
        if (index != -1)
            result = s.substring(index + 1, s.length());
        return result;
    }

    public static String getTimeStamp() {
        int value = 0;
        String ts = "";
        String month[] = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT+07:00");
        cal.setTimeZone(tz);

        ts += cal.get(Calendar.YEAR);
        ts += month[cal.get(Calendar.MONTH)];
        value = cal.get(Calendar.DAY_OF_MONTH);
        if (value < 10) {
            ts += "0" + value;
        } else {
            ts += value;
        }
        value = cal.get(Calendar.HOUR_OF_DAY);
        if (value < 10) {
            ts += "0" + value;
        } else {
            ts += value;
        }
        value = cal.get(Calendar.MINUTE);
        if (value < 10) {
            ts += "0" + value;
        } else {
            ts += value;
        }
        value = cal.get(Calendar.SECOND);
        if (value < 10) {
            ts += "0" + value;
        } else {
            ts += value;
        }

        ts = "TTTTTTTTTTTTTT" + ts;
        ts = ts.substring(ts.length() - 14);

        return ts;
    }

    public static String[] split(String text, String delimiter) {
        String[] result = new String[]{"", ""};
        text = text.trim();
        if (text.length() > 0) {
            int index = text.indexOf(delimiter);
            if (index >= 0) {
                result[0] = text.substring(0, index);
                result[1] = text.substring(index + delimiter.length(), text.length());
            } else {
                result[0] = text;
            }
        }
        return result;
    }
    private static Hashtable table = new Hashtable();

    static {
        table.put("&#193;", "Á");
        table.put("&#225;", "á");
        table.put("&#192;", "À");
        table.put("&#224;", "à");
        table.put("&#7842;", "Ả");
        table.put("&#7843;", "ả");
        table.put("&#195;", "Ã");
        table.put("&#227;", "ã");
        table.put("&#7840;", "Ạ");
        table.put("&#7841;", "ạ");
        table.put("&#258;", "Ă");
        table.put("&#259;", "ă");
        table.put("&#7854;", "Ắ");
        table.put("&#7855;", "ắ");
        table.put("&#7856;", "Ằ");
        table.put("&#7857;", "ằ");
        table.put("&#7858;", "Ẳ");
        table.put("&#7859;", "ẳ");
        table.put("&#7860;", "Ẵ");
        table.put("&#7861;", "ẵ");
        table.put("&#7862;", "Ặ");
        table.put("&#7863;", "ặ");
        table.put("&#194;", "Â");
        table.put("&#226;", "â");
        table.put("&#7844;", "Ấ");
        table.put("&#7845;", "ấ");
        table.put("&#7846;", "Ầ");
        table.put("&#7847;", "ầ");
        table.put("&#7848;", "Ẩ");
        table.put("&#7849;", "ẩ");
        table.put("&#7850;", "Ẫ");
        table.put("&#7851;", "ẫ");
        table.put("&#7852;", "Ậ");
        table.put("&#7853;", "ậ");
        table.put("&#201;", "É");
        table.put("&#194;", "Â");
        table.put("&#233;", "é");
        table.put("&#200;", "È");
        table.put("&#232;", "è");
        table.put("&#7866;", "Ẻ");
        table.put("&#7867;", "ẻ");
        table.put("&#7868;", "Ẽ");
        table.put("&#7869;", "ẽ");
        table.put("&#7864;", "Ẹ");
        table.put("&#7865;", "ẹ");
        table.put("&#202;", "Ê");
        table.put("&#234;", "ê");
        table.put("&#7870;", "Ế");
        table.put("&#7871;", "ế");
        table.put("&#7872;", "Ề");
        table.put("&#7873;", "ề");
        table.put("&#7874;", "Ể");
        table.put("&#7875;", "ể");
        table.put("&#7876;", "Ễ");
        table.put("&#7877;", "ễ");
        table.put("&#7878;", "Ệ");
        table.put("&#7879;", "ệ");
        table.put("&#218;", "Ú");
        table.put("&#250;", "ú");
        table.put("&#217;", "Ù");
        table.put("&#249;", "ù");
        table.put("&#7910;", "Ủ");
        table.put("&#7911;", "ủ");
        table.put("&#360;", "Ũ");
        table.put("&#361;", "ũ");
        table.put("&#7908;", "Ụ");
        table.put("&#7909;", "ụ");
        table.put("&#431;", "Ư");
        table.put("&#432;", "ư");
        table.put("&#7912;", "Ứ");
        table.put("&#7913;", "ứ");
        table.put("&#7914;", "Ừ");
        table.put("&#7915;", "ừ");
        table.put("&#7916;", "Ử");
        table.put("&#7917;", "ử");
        table.put("&#7918;", "Ữ");
        table.put("&#7919;", "ữ");
        table.put("&#7920;", "Ự");
        table.put("&#7921;", "ự");
        table.put("&#211;", "Ó");
        table.put("&#243;", "ó");
        table.put("&#210;", "Ò");
        table.put("&#242;", "ò");
        table.put("&#7886;", "Ỏ");
        table.put("&#7887;", "ỏ");
        table.put("&#213;", "Õ");
        table.put("&#245;", "õ");
        table.put("&#7884;", "Ọ");
        table.put("&#7885;", "ọ");
        table.put("&#212;", "Ô");
        table.put("&#244;", "ô");
        table.put("&#7888;", "Ố");
        table.put("&#7889;", "ố");
        table.put("&#7890;", "Ồ");
        table.put("&#7891;", "ồ");
        table.put("&#7892;", "Ổ");
        table.put("&#7893;", "ổ");
        table.put("&#7894;", "Ỗ");
        table.put("&#7895;", "ỗ");
        table.put("&#7896;", "Ộ");
        table.put("&#7897;", "ộ");
        table.put("&#416;", "Ơ");
        table.put("&#417;", "ơ");
        table.put("&#7898;", "Ớ");
        table.put("&#7899;", "ớ");
        table.put("&#7900;", "Ờ");
        table.put("&#7901;", "ờ");
        table.put("&#7902;", "Ở");
        table.put("&#7903;", "ở");
        table.put("&#7904;", "Ỡ");
        table.put("&#7905;", "ỡ");
        table.put("&#7906;", "Ợ");
        table.put("&#7907;", "ợ");
        table.put("&#272;", "Đ");
        table.put("&#273;", "đ");
        table.put("&#205;", "Í");
        table.put("&#237;", "í");
        table.put("&#204;", "Ì");
        table.put("&#236;", "ì");
        table.put("&#7880;", "Ỉ");
        table.put("&#7881;", "ỉ");
        table.put("&#296;", "Ĩ");
        table.put("&#297;", "ĩ");
        table.put("&#7882;", "Ị");
        table.put("&#7883;", "ị");
        table.put("&#221;", "Ý");
        table.put("&#253;", "ý");
        table.put("&#7922;", "Ỳ");
        table.put("&#7923;", "ỳ");
        table.put("&#7926;", "Ỷ");
        table.put("&#7927;", "ỷ");
        table.put("&#7928;", "Ỹ");
        table.put("&#7929;", "ỹ");
        table.put("&#7924;", "Ỵ");
        table.put("&#7925;", "ỵ");
        table.put("&#208;", "Ð");
    }

    ;

    public static String convertHTMLtoString(String text) {
        String output = "";

        int index = text.indexOf("&#");
        int start = 0, end = 0;

        if (index != -1) {
            StringBuffer buffer = new StringBuffer();

            do {
                end = index;
                String s = text.substring(start, end);
                buffer.append(s);

                int ind = text.indexOf(";", end);
                if (ind != -1) {
                    String s2 = text.substring(end, ind + 1);
                    String s3 = table.get(s2).toString();
                    buffer.append(s3);

                    start = ind + 1;
                    index = text.indexOf("&#", ind);

                    if (index == -1) {
                        buffer.append(text.substring(start, text.length()));
                    }

                } else {

                    buffer.append(text.substring(end, text.length()));
                    break;
                }

            } while (index != -1);

            output = buffer.toString();


        } else {
            output = text;
        }
        return output;
    }
}
